package org.example.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.reggie.entry.Category;
import org.example.reggie.entry.Dish;
import org.example.reggie.entry.Setmeal;
import org.example.reggie.exception.CustomerException;
import org.example.reggie.mapper.CategoryMapper;
import org.example.reggie.service.CategoryService;
import org.example.reggie.service.DishService;
import org.example.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 菜品及套餐分类 service接口实现类
 * 继承mybatis-plus：extends ServiceImpl
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    public DishService dishService;
    @Autowired
    public SetmealService setmealService;
    /**
     * 删除之前，判断段是否已经被其他模块引用
     * @param id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> queryWdish = new LambdaQueryWrapper<>();
        queryWdish.eq(Dish::getCategoryId,id);
        int count = (int) dishService.count(queryWdish);
        if(count > 0){
            //抛出自定义异常，然后再全局异常中获取，返回到页面
           throw new CustomerException("当前分类下已关联菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> queryWsetmeal = new LambdaQueryWrapper<>();
        queryWsetmeal.eq(Setmeal::getCategoryId,id);
        int count2 = (int) setmealService.count(queryWsetmeal);
        if(count2 > 0){
             //抛出自定义异常，然后再全局异常中获取，返回到页面
            throw new CustomerException("当前分类下已关联套餐，不能删除");
        }
        super.removeById(id);
    }
}
